package com.itheima.web.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Dept;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.DeptService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

@WebServlet("/system/dept/*")
public class DeptServlet extends BaseServlet {
//    @Override
//    protected Result doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, Exception {
//        //1 获取请求标记
//        //获取Servlet一级目录后面的路径 ，也就是/list、/save、/edit、/delete 等
//        String info = req.getPathInfo().substring(1);
//        //2 判断标记，调用不同的方法处理请求
//        if ("findByPage".equals(info)){
//            findByPage(req,resp);
//        }else if ("save".equals(info)){
//            save(req,resp);
//        }else if ("findById".equals(info)){
//            findById(req,resp);
//        }else if ("edit".equals(info)){
//            edit(req,resp);
//        }else if ("delete".equals(info)){
//            delete(req,resp);
//        }else if ("findAll".equals(info)){
//            findAll(req,resp);
//        }
//    }
private    DeptService deptService = BeanFactory.getService("deptService", DeptService.class);

    private Result findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //2 调用service层方法获取分页结果
            List<Dept> list= deptService.findAll();
            //3 对客户的作出响应
            return new Result(true,MessageConstant.QUERY_DEPT_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_DEPT_FAIL);
        }
    }

    private Result delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数并封装成Dept对象
            String[] ids = BeanUtil.fillBeanFromJson(req, String[].class);
            //2 调用service层方法添加数据
            deptService.delete(ids);
            //3 响应结果
            return new Result(true,MessageConstant.DELETE_DEPT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_DEPT_FAIL);
        }
    }

    private Result edit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数并封装成Dept对象
            Dept dept = BeanUtil.fillBeanFromJson(req, Dept.class);
            //2 调用service层方法添加数据
            deptService.update(dept);
            //3 响应结果
            return new Result(true,MessageConstant.EDIT_DEPT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_DEPT_FAIL);
        }
    }

    private Result findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数
            String id = req.getParameter("id");
            //2 调用service层方法获取分页结果
            Dept dept = deptService.findById(id);
            //3 对客户的作出响应
            return new Result(true,MessageConstant.QUERY_DEPT_SUCCESS,dept);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_DEPT_FAIL);
        }
    }

    private Result save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数并封装成Dept对象
            Dept dept = BeanUtil.fillBeanFromJson(req, Dept.class);
            //2 调用service层方法添加数据
            deptService.save(dept);
            //3 响应结果
            return new Result(true,MessageConstant.SAVE_DEPT_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_DEPT_FAIL);
        }
    }

    /*
     * 处理分页查询的请求
     * @param request
     * @param response
     */
    private Result findByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数（当前页数和每页条数）
            String currentPage1 = req.getParameter("currentPage");
            String pageSize1 = req.getParameter("pageSize");
            //2.判断是否为空
            int currentPage = 1,pageSize = 5;
            if (!StringUtils.isBlank(currentPage1)){
                //3.给页码和条数赋默认值
                currentPage = Integer.parseInt(currentPage1);

            }
            if (!StringUtils.isBlank(pageSize1)){
                //3.给页码和条数赋默认值
                pageSize = Integer.parseInt(pageSize1);

            }
            //4.调用Service层方法获得结果
            PageInfo<Dept> page = deptService.findByPage(currentPage, pageSize);
            //5.给客户端响应
            return new Result(true,MessageConstant.QUERY_DEPT_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_DEPT_FAIL);
        }
    }

//    @Override
//    protected Result doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, Exception {
//        doGet(req, resp);
//    }


}

